export default {
    data() {
        return {
            contentList: [], // 商品列表
            classyIndex: 0, // 产品分类索引
            pages: 1, // 当前页码
            pageSize: 1000, // 每页条目数量
            noMore: false, // 没有更多数据了
        }
    },
    onLoad() {
        this.getPageData();
    },
    onShow() {
        if (this.$store.state.replenishNumner) { // 如果有存在未填写的上货单
            uni.showModal({
                title: '提示',
                content: '您有未填写提交的上货单，是否继续填写提交？放弃后不可再次操作！',
                showCancel: true,
                cancelText: '放弃填写',
                confirmText: '继续填写',
                success: res => {
                    if (res.confirm) {
                        uni.navigateTo({
                            url: 'addStock/addStock'
                        });
                    } else {
                        this.$store.state.replenishNumner = ''; // 清除全局上货单号
                    }
                }
            });
        }
    },
    methods: {

        // 获取商品信息
        getPageData: function() {
            let data = {}
            let url = "";
            if (this.$store.state.deviceId) { // 有设备id时
                data.deviceId = this.$store.state.deviceId, // 设备id
                    data.pageNo = this.pages; // 页码
                data.pageSize = this.pageSize; // 长度
                url = this.$api.GetGoodsListByDevice;
                this.$util.request(url, data, "GET").then(res => {
                    if (res.code == 200) {
                        // res.data = res.data || [];
                        res.data = res.result.records || [];
                        let contentList = [{
                            typeName: '全部',
                            type: '',
                            list: []
                        }];
                        let hasType = ['全部'];
                        if (res.data.length) {
                            res.data.forEach((item, i) => {
                                item.price = Number(item.price).toFixed(2)
                                contentList[0].list.push(item);
                                let index = hasType.indexOf(item.typeName);
                                if (index != -1) {
                                    contentList[index].list.push(item);
                                } else {
                                    hasType.push(item.typeName);
                                    contentList.push({
                                        typeName: item.typeName,
                                        type: item.type,
                                        list: []
                                    })
                                    contentList[hasType.indexOf(item.typeName)].list.push(item);
                                }
                            })
                        }
                        this.contentList = contentList;
                        if (res.data.length < this.pageSize) {
                            this.noMore = true;
                        }
                    } else {
                        uni.showToast({
                            title: res.returnMessage,
                            icon: "none"
                        });
                    }
                })
            } else { // 无设备id时
                uni.showModal({
                    title: '提示',
                    content: '请扫描设备上的二维码，否则将无法使用',
                    showCancel: false,
                    confirmText: '扫码',
                    success: res => {
                        if (!res.confirm) {
                            return;
                        }
                        uni.scanCode({
                            onlyFromCamera: false,
                            success: (res) => {
                                this.$util.extractUri(this, res.result)
                                this.getPageData();
                            },
                            complete: (res) => {
                                console.log(res)
                            }
                        });
                    }
                });
            }

        },
        //                    
        scrollEnd: function() {
            if (this.noMore == false) {
                this.pages++;
                this.getPageData();
            }
        },

        // 首页点击
        home: function() {
            uni.redirectTo({
                url: '/pages/indexManage/indexManage',
            });
        },

        // 分类选择
        classySelect: function(index) {
            this.classyIndex = index;
        },

        // 点击进入个人中心
        personal: function() {
            uni.navigateTo({
                url: '/pages/mine/mine'
            });
        },

        // 上货库存
        stock: function() {
            uni.redirectTo({
                url: 'stock/stock'
            });
        },

        // 上货记录
        record: function() {
            uni.redirectTo({
                url: 'record/record'
            });
        },

        // 上货
        add: function() {
            if (this.$store.state.replenishNumner) { // 如果有存在未填写的上货单
                uni.showModal({
                    title: '提示',
                    content: '您有未填写提交的上货单，是否继续填写提交？放弃后不可再次操作！',
                    showCancel: true,
                    cancelText: '放弃填写',
                    confirmText: '继续填写',
                    success: res => {
                        if (res.confirm) {
                            uni.navigateTo({
                                url: 'addStock/addStock'
                            });
                        } else {
                            this.$store.state.replenishNumner = ''; // 清除全局上货单号
                        }
                    }
                });
                return false;
            }

            // 请求参数
            let data = {
                deviceId: this.$store.state.deviceId,
                openType: 0, // 上货开门请求
            }
            
            let doorIndex = this.$store.state.doorIndex;
            if (typeof(doorIndex) != 'undefined' && doorIndex != null) {
                data.doorIndex = doorIndex
            }
            // 上货接口
            this.$request(this.$api.OpenDoor, data, "GET").then((res) => {
                if (res.code == 200) {
                    // let data = JSON.parse(res.data);
                    this.$store.state.replenishNumner = res.result.activityId; // 保存记录id
                    this.$store.state.activityRecordId = res.result.orderId; // 保存开门记录id
                    console.log(190, this.$store.state.activityRecordId);


                    uni.navigateTo({
                        url: 'addStock/addStock?activityRecordId=' + this.$store.state
                            .activityRecordId
                    });
                } else {
                    uni.showModal({
                        title: '开门失败',
                        content: res.message,
                        showCancel: false,
                    });
                }
            })
        },

    }
}